<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Classify extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        
        $this->data['controller'] = 'classify';         
        $this->load->model('Mclassify');
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->checkPermission($this->session->userdata('level'), array(1));
    }
    
    public function listClassify()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('search', 'Từ khóa', 'required');
        
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] = 20;
        $this->data['page'] = $this->getParamUri(2,3);
        
        if(!in_array($this->getParamString('search'), array(null, '')))
        {
            $data = $this->Mclassify->getSearchContent($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$this->getParamString('search'));
            $config['paramhack'] = '?search='.$this->getParamString('search');
        }else{
            $data = $this->Mclassify->getListClassify($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0));
        }
        
        $config['base_url'] = base_url($this->uri->segment(1));
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
		if($this->form_validation->run() === TRUE)
        {
            $keyword = $this->getParamString('search');
            
            $this->data['id_active'] = $this->getParamUri(2,3);
            $this->load->library('pagination');
            $config['per_page'] = $this->data['per_page'] = 20;
            $this->data['page'] = $this->getParamUri(2,3);
            $data = $this->Mclassify->getSearchContent($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$keyword);
            $config['base_url'] = base_url($this->uri->segment(1));
            $config['total_rows'] = $data['count'];
            $config['uri_segment'] = 2;
            $config['uri_segment_page'] = $this->getParamUri(2,3);
            $config['suffix'] = '.html';
            $config['paramhack'] = '?search='.$keyword;
            $this->pagination->initialize($config);
     
            $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
            $this->data['data'] = $data['data'];
        }
        
        $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addClassify()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('os_name', 'Hệ điều hành', 'required');
        $this->form_validation->set_rules('device_name', 'Tên thiết bị', 'required');
        $this->form_validation->set_rules('note', 'Tên thương mại', 'required');
        $this->form_validation->set_rules('classify', 'Nhóm khách hàng', 'required');
        $this->form_validation->set_rules('type', 'Bộ lọc', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $os_name = $this->getParamString('os_name');
            $device_name = $this->getParamString('device_name');
            $note = $this->getParamString('note');
            $classify = $this->getParamInt('classify');
            $type = $this->getParamInt('type');
            
            $this->data['return'] = $this->Mclassify->addClassifyDb($os_name,$device_name,$note,$classify,$type);
            if($this->data['return'] == 200){
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'add-classify";</script>');
            }
        }
        
        $this->data['action'] = 'addclassify';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editClassify()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('os_name', 'Hệ điều hành', 'required');
        $this->form_validation->set_rules('device_name', 'Tên thiết bị', 'required');
        $this->form_validation->set_rules('note', 'Tên thương mại', 'required');
        $this->form_validation->set_rules('classify', 'Nhóm khách hàng', 'required');
        $this->form_validation->set_rules('type', 'Bộ lọc', 'required');
        
        $id = $this->getParamUri(2,1);
        $this->data['data'] = $this->Mclassify->getInfo($id);
        
        if($this->form_validation->run() === TRUE)
        {
            $os_name = $this->getParamString('os_name');
            $device_name = $this->getParamString('device_name');
            $note = $this->getParamString('note');
            $classify = $this->getParamInt('classify');
            $type = $this->getParamInt('type');
            
            $this->data['return'] = $this->Mclassify->editClassifyDb($os_name,$device_name,$note,$classify,$type,$id);
            if($this->data['return'] == 200){
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'edit-classify/'.$id.'";</script>');
            }
        }
        
        $this->data['action'] = 'editclassify';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deleteClassify()
    {
        $id = $this->getParamUri(2,1);
        die(json_encode($this->Mclassify->deleteClassifyDb($id)));
    }
}
